package com.jd.jr.insights.dm.dim.dao.impl;


import com.jd.jr.insights.dm.dim.dao.DeptDao;


import com.jd.jr.insights.dm.dim.dao.model.JfDimDept;
import com.jd.jr.insights.dm.dim.domain.Department;
import com.jd.jr.insights.dm.dim.util.fastjson.tree.ITreeNode;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Created by guoxuefei1 on 2017/7/18.
 */
public class DeptDaoImpl implements DeptDao {
    public List<ITreeNode> getList() {
        List<JfDimDept>JfDimDepts = JfDimDept.dao.find("select * from jf_dim_dept where is_valid=1");
        List<ITreeNode> depts=new ArrayList<ITreeNode>(JfDimDepts.size());
        Iterator<JfDimDept> inter=JfDimDepts.iterator();
        while(inter.hasNext()){
            JfDimDept deptM=inter.next();
            Department dept=new Department (deptM.getId()
                    ,deptM.getCode()
                    ,deptM.getValue()
                    ,deptM.getDesc()
                    ,deptM.getLevel()
                    ,deptM.getParentId()
                    ,deptM.getIsValid()
                    ,deptM.getCreatedTime()
                    ,deptM.getUpdatedTime()
            );
            depts.add(dept);
        }

        return depts;
    }

    public Boolean deleteById(Department dept) {
        JfDimDept daoa=JfDimDept.dao.findById(dept.getId());
        daoa.setIsValid(0);
        daoa.setUpdatedTime(new Date());
        Boolean result=daoa.update();
        return result;
    }

    public Boolean update(Department dept) {
        JfDimDept daoa=JfDimDept.dao.findById(dept.getId());
        daoa.setCode(dept.getCode());
        daoa.setValue(dept.getValue());
        daoa.setDesc(dept.getDesc());
        daoa.setUpdatedTime(new Date());
        Boolean result=daoa.update();


        return result;
    }

    public Boolean save(Department dept) {
        JfDimDept daoa=new JfDimDept();
        daoa.setCode(dept.getCode());
        daoa.setValue(dept.getValue());
        daoa.setDesc(dept.getDesc());
        daoa.setLevel(dept.getLevel());
        daoa.setIsValid(1);
        daoa.setParentId(dept.getParent_id());
        daoa.setCreatedTime(new Date());
        daoa.setUpdatedTime(new Date());
        Boolean result=daoa.save();
        return result;
    }
}
